/**
 * Created by Administrator on 2017/9/19.
 */
var resetItemClickListener  = function () {

    $('.item-detail').click(function(e) {
        var orderId  = $(this).parents(".menu-item").attr("Order-id");
        var tableNum = $(this).next(".tableNum").attr("value");
        var name     = $(this).children(".name").attr("item-name");
        var count    = $(this).nextAll(".count").attr("value");
        var price    = $(this).children(".price").attr("item-price");
        var dish     = $(this).nextAll(".dish").attr("value");

        var orderStatus    = $(this).nextAll(".orderStatus").attr("value");

        if (orderStatus != 0) {
            alert("当前桌号已结账");
            return false;
        }

        //弹出框设置
        $('.revealTableNum').html("第 "+tableNum+" 桌");

        var jsonStr = $('#inputData').val();
        var jsonObj = JSON.parse(jsonStr);
        var dishsHtml = "";
        for (var i = 0; i < jsonObj.length; i ++) {
            if (jsonObj[i].orderId == orderId) {
                var dishs = jsonObj[i].dish;
                for (var j = 0; j < dishs.length; j ++) {
                    dishsHtml +=
                        "<li class='checkout-tablerow'>"+
                            "<div class='cell itemname  text_align_left' >"+dishs[j].name+"</div>"+
                            "<div class='cell itemquantity  text_align_left'  style='text-align: center'>"+dishs[j].count+"</div>"+
                            "<div class='cell itemtotal  text_align_left'  style='text-align: center;'>"+dishs[j].price+"</div> " +
                        "</li>";
                }
            }//if
        }//for

        dishsHtml += "<input class='orderId'  type='hidden' value='"+orderId+"'></input>"
                   + "<input class='tableNum' type='hidden' value='"+tableNum+"'></input>";

        //弹出框中的内容
        $('#checkout-body').html(dishsHtml);
        //弹出
        $('#header-login').trigger('click');

        //console.log(dish);
    });

    $('.header-left').click(function(e) {
        dialog.confirm("确定要退出登陆吗？", exitPath);
    });
};


var checkout = function(orderId, tableNum) {
    var index = layer.load();
    $.ajax({
        type:"get",
        url:apiCheckout,
        //url:"testData.json",
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("确认订单失败：请稍后再试");
            //alert("确认订单失败：请稍后再");
            console.log("确认订单失败：失败原因："+XMLHttpRequest+">>"+textStatus+">>"+errorThrown.toString());
            layer.close(index);
        },
        data: {
            "orderId":orderId,
            "tableNum":tableNum
        },
        dataType: "json",
        success:function(data) {
            console.log(data.toString());
            switch (data.code) {
                case 30001 :
                    onSuccessCheckout(data);
                    break;
                case 31001 :
                    onFailCheckout(data);
                    break;
            }
            layer.close(index);
            //location.href="order_confirm_new.html";
        }
    });
};

var onSuccessCheckout = function(data) {
    alert(data.message);

    reqOrderList();
};

var onFailCheckout = function(data) {
    alert(data.message);
    reqOrderList();
};

//重设弹出框
var resetReveal = function() {
    $('#checkout-body').html("");
};

(function($){
    //关闭按钮
    $('#checkout').click(function(){
        var eleOrderId = $("#loginform").find(".orderId");
        var eleTableNum = $("#loginform").find(".tableNum");

        var orderId = eleOrderId.attr("value");
        var tableNum = eleTableNum.attr("value");

        console.log(eleOrderId.toString()+"\n"+eleOrderId.attr("value"));
        console.log(eleTableNum.toString()+"\n"+eleOrderId.attr("value"));


        //确认订单
        checkout(orderId,tableNum);
        //弹出层关闭
        $('.close-reveal-modal').trigger('click');
        //TODO:看下要不要清空 弹出框数据
    });

    resetItemClickListener();
})(jQuery);